IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RemoveLogItemsForProcess]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[RemoveLogItemsForProcess]

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/***************************************************************************
* Name:			RemoveLogItemsForProcess
* Purpose:		Delete log items
*
* PARAMETERS
* Name				Description					
* -------------		-----------------------------------------------------------
@protocolProcessId	Protocol process id for the logItem to be deleted


***************************************************************************/
CREATE PROCEDURE [dbo].[RemoveLogItemsForProcess]
(
@protocolProcessId int
)

AS BEGIN

SET NOCOUNT ON

	DECLARE @TempLogItemIds TABLE(
	[LogItemId] [int] NOT NULL
	)

	INSERT INTO @TempLogItemIds (LogItemId)
	SELECT LogItemId 
	FROM ProtocolProcess_LogItem_Map
	WHERE ProtocolProcessId = @protocolProcessId

	DELETE FROM ProtocolProcess_LogItem_Map WHERE ProtocolProcessId = @protocolProcessId

	DELETE FROM LogItem Where 
	Id in (Select LogItemId from @TempLogItemIds)

	IF @@Error<>0 
	RAISERROR('Unable to remove LogItems for the specfied process', 16, 1)
	RETURN

END
GO
